// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.gameScene = ck.getGameScene()
    },

    update() {
        if (this.gameScene.data.state === ck.GameState.Prepare)
            return
        if (this.gameScene.data.state === ck.GameState.TouchDoor)
            return
        if (this.gameScene.data.state === ck.GameState.Finished)
            return
        if (this.gameScene.data.state === ck.GameState.Dead)
            return

        if (this.gameScene.player.getWorldPosition().y<this.getWorldLeftBottom().y) {
            this.gameScene.player.setDead(true)
        }
    },

    getWorldLeftBottom() {
        var local = new cc.Vec2(-this.node.width/2, -this.node.height/2)
        return this.node.convertToWorldSpaceAR(local)
    },

    getWorldRightBottom() {
        var local = new cc.Vec2(this.node.width/2, -this.node.height/2)
        return this.node.convertToWorldSpaceAR(local)
    },

    getLeftBottomRelativeNodeParent(node) {
        var world = this.getWorldLeftBottom()
        return this.node.parent.convertToNodeSpaceAR(world)
    },

    getRightBottomRelativeNodeParent(node) {
        var world = this.getWorldRightBottom()
        return this.node.parent.convertToNodeSpaceAR(world)
    },

    width() {
        return this.node.width
    },

    height() {
        return this.node.height
    },
});
